package com.dodayum.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dodayum.entity.RealtimeEntity;
import com.dodayum.mapper.RealtimeMapper;
import com.dodayum.vo.RealtimeQueryNew50VO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class RealtimeService {

    @Resource
    private RealtimeMapper realtimeMapper;

    /*
    入参50个
    根据word去数据库里搜
    去掉已经有的，其余的插进去
     */

    public void insertNewRealtimeList(List<RealtimeEntity> realtimeEntityList) {
        Map<String, RealtimeEntity> realtimeMap = new HashMap<>();
        realtimeEntityList.forEach(realtimeEntity -> realtimeMap.put(realtimeEntity.getWord(), realtimeEntity));
        Set<String> realtimeWords = realtimeMap.keySet();
        LambdaQueryWrapper<RealtimeEntity> realtimeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        realtimeLambdaQueryWrapper.in(RealtimeEntity::getWord, realtimeWords);
        List<RealtimeEntity> oldRealtimeEntityList = realtimeMapper.selectList(realtimeLambdaQueryWrapper);
        oldRealtimeEntityList.forEach(realtimeEntity -> realtimeMap.remove(realtimeEntity.getWord()));
        Collection<RealtimeEntity> values = realtimeMap.values();
        for (RealtimeEntity value : values) {
            realtimeMapper.insert(value);
            log.info(value.getWord());
        }
        if (CollectionUtils.isNotEmpty(values)) {
            log.info("=======================\n共更新{}条", values.size());
        }

    }

    public Long getCount() {
       return realtimeMapper.selectCount(new LambdaQueryWrapper<>());
    }

    public List<RealtimeQueryNew50VO> getNew50() {
        return realtimeMapper.queryNew50();
    }
}
